package com.ruoyi.flowable.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.flowable.domain.TaskVo;
import com.ruoyi.flowable.service.ITaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@RequestMapping("/flowable/task")
public class TaskController extends BaseController {


    @Resource
    private ITaskService taskService;


    @SaCheckPermission("flowable:task:todoList")
    @GetMapping(value = "/todoList")
    public TableDataInfo todoList(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(name = "taskName", required = false) String taskName) {
        return taskService.todoList(pageNum, pageSize, taskName);
    }

    @SaCheckPermission("flowable:task:finishList")
    @GetMapping(value = "/finishList")
    public TableDataInfo finishList(@RequestParam Integer pageNum,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(name = "taskName", required = false) String taskName) {
        return taskService.finishList(pageNum, pageSize, taskName);
    }


    /**
     * 获取任务详细信息
     */
    @SaCheckPermission("flowable:task:query")
    @GetMapping(value = "/{taskId}")
    public R<TaskVo> getInfo(@PathVariable("taskId") String taskId) {
        return R.ok(taskService.selectTaskByTaskId(taskId));
    }

    /**
     * 获取流程历史流转记录
     */
    @GetMapping(value = "/taskRecord/{procInstId}")
    public R<Map<String, Object>> taskRecord(@PathVariable("procInstId") String procInstId) {
        return R.ok(taskService.taskRecord(procInstId));
    }


    /**
     * 审批任务
     */
    @SaCheckPermission("flowable:task:complete")
    @PostMapping(value = "/complete")
    public R<Void> complete(@RequestBody TaskVo taskVo) {
        taskService.complete(taskVo);
        return R.ok("审批完成");
    }

    /**
     * 驳回发起者修改
     */
    @SaCheckPermission("flowable:task:reject")
    @PostMapping(value = "/reject")
    public R<Void> taskReject(@RequestBody TaskVo taskVo) {
        taskService.taskReject(taskVo);
        return R.ok();
    }

    /**
     * 结束流程
     */
    @SaCheckPermission("flowable:task:stop")
    @PostMapping(value = "/stopProcess")
    public R<Void> stopProcess(@RequestBody TaskVo taskVo) {
        taskService.stopProcess(taskVo);
        return R.ok();
    }
}
